package net.siufung.security.provider;

import com.google.common.collect.Lists;
import lombok.Getter;
import net.siufung.security.provider.dto.AccessTokenLoginDto;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @author 陈建峰
 * @since 2021/11/17 11:12 上午
 */
public class AccessTokenAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = 5718680626420882182L;
    /**
     * 用户主体
     */
    private Object principal;

    /**
     * 认证证书
     */
    private String credential;

    /**
     * 令牌登录对象
     */
    @Getter
    private AccessTokenLoginDto accessTokenLoginDto;

    /**
     * 登录构造
     * @param accessTokenLoginDto 登录Dto
     */
    public AccessTokenAuthenticationToken(AccessTokenLoginDto accessTokenLoginDto){
        super(Lists.newArrayList());
        this.accessTokenLoginDto = accessTokenLoginDto;
    }

    /**
     * 授权构造
     * @param principal 授权主题
     * @param credentials 授权密钥
     * @param authorities 权限集合
     */
    public AccessTokenAuthenticationToken(Object principal, String credentials, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credential = credentials;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.credential;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public void eraseCredentials() {
        this.credential = null;
    }
}
